angular.module('mvcApp.product', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/product/:productId', {
            templateUrl: 'static/views/product.html',
            controller: 'ProductCtrl'
        });
}]).controller('ProductCtrl', ['$scope', '$http', '$location', 'User', '$routeParams', 'Shop', 'Category', 'Product',
    function ($scope, $http, $location, User, $routeParams, Shop, Category, Product) {
        $scope.productId = $routeParams.productId;
        Product.getProduct($scope.productId)
            .then(function(product) {
                $scope.product = product;
                $scope.$apply();
            }).catch(function(err) {
                if(err) {
                    alert(err);
                }
            });
        $scope.comments = [];
        $scope.isCollected = false;
        $scope.loadCollectedInfo =function () {
            Product.hasCollectedProduct($scope.productId)
                .then(function(hasCollected) {
                    $scope.isCollected = hasCollected;
                    $scope.$apply();
                }).catch(function() {});
        };
        $scope.loadCollectedInfo();
        $scope.collect = function() {
            Product.collectProduct($scope.productId)
                .then(function() {
                    $scope.loadCollectedInfo();
                }).catch(function(err) {
                    alert(err);
                });
        };
        $scope.unCollect = function() {
            Product.unCollectProduct($scope.productId)
                .then(function() {
                    $scope.loadCollectedInfo();
                }).catch(function(err) {
                    alert(err);
                });
        };
        $scope.loadComments = function() {
            Product.getLatestProductComments($scope.productId)
                .then(function(comments) {
                    $scope.comments = comments;
                    _.each($scope.comments, function(comment) {
                        User.getUserInfoWithCache(comment.userId)
                            .then(function(userInfo) {
                                comment.user = userInfo;
                                $scope.$apply();
                            }).catch(function(err) {});
                    });
                    $scope.$apply();
                }).catch(function(err) {
                    if(err) {
                        alert(err);
                    }
                });
        };
        $scope.loadComments();
        $scope.addProductComment = _.throttle(function(title, content) {
            if(!content) {
                alert("请填入评论");
                return;
            }
            Product.addProductComment($scope.productId, title, content)
                .then(function() {
                    $scope.loadComments();
                }).catch(function(err) {
                    alert(err);
                });
        }, 3000);
    }]);